package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class AreaAction extends ActionSupport implements ModelDriven<Area> {
    private Area area = new Area();
    @Override
    public Area getModel() {
        return area;
    }

    @Autowired
    private AreaService areaService;



    //实现区域数据批量导入
    /*属性驱动，解析Excel逻辑
        工作簿-->sheet-->row-->cell
     */
    private File file;
    private String fileFileName;
    public void setFile(File file) {
        this.file = file;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    @Action(value = "area_batchImport")
    public String batchImport(){
        areaService.uploadArea(file,fileFileName);
        return NONE;
    }


    private int page;
    private int rows;

    public void setPage(int page) {
        this.page = page;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }

    // 分页查询
    @Action(value = "area_pageQuery", results = { @Result(name = "success", type = "json") })
    public String pageQuery() {
        // 构造分页查询对象
        Pageable pageable = new PageRequest(page - 1, rows);
        // 构造条件查询对象
        Specification<Area> specification = new Specification<Area>() {
            @Override
            public Predicate toPredicate(Root<Area> root,
                                         CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (StringUtils.isNotBlank(area.getProvince())) {
                    Predicate p1 = cb.like(root.get("province")
                            .as(String.class), "%" + area.getProvince() + "%");
                    list.add(p1);
                }
                if (StringUtils.isNotBlank(area.getCity())) {
                    Predicate p2 = cb.like(root.get("city").as(String.class),
                            "%" + area.getCity() + "%");
                    list.add(p2);
                }
                if (StringUtils.isNotBlank(area.getDistrict())) {
                    Predicate p3 = cb.like(root.get("district")
                            .as(String.class), "%" + area.getDistrict() + "%");
                    list.add(p3);
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        // 调用业务层完成查询
        Page<Area> pageData = areaService.findPageData(specification, pageable);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",pageData.getTotalElements());
        map.put("rows",pageData.getContent());
        // 压入值栈
        ActionContext.getContext().getValueStack().push(map);
        return SUCCESS;
    }


}
